package cn.rongdao.jrkabao.ui.cashadvance.manager

import android.content.Context
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import cn.rongdao.jrkabao.R
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity
import cn.rongdao.jrkabao.ui.cashadvance.fragment.FragmentCredit
import cn.rongdao.jrkabao.ui.cashadvance.fragment.FragmentDepositCard
import com.mifengkong.frtools.ui.activity.FRStartActivity
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent
import com.mifengkong.kotlincommon.dp
import com.mifengkong.kotlincommon.indicator.IndicatorAdapter
import kotlinx.android.synthetic.main.activity_cash_advance_manager.*
import java.util.*

/**
 * Created by jack on 2018/6/4
 */
class CashAdvanceManagerActivity : BaseAutoFinishActivity() {

    companion object {
        fun start(context: Context) {
            FRStartActivity.start(context, CashAdvanceManagerActivity::class.java)
        }
    }

    private var mFragmentList = ArrayList<Fragment>()
    private var mTitleList = arrayListOf("信用卡", "储蓄卡")
    private lateinit var titleBarUIComponent: TitleBarUIComponent

    private var isChange: Boolean = false

    override fun layoutRes(): Int {
        return R.layout.activity_cash_advance_manager
    }

    override fun initView() {
        super.initView()
        titleBarUIComponent = acamTitlebar as TitleBarUIComponent
        initTitleBar()

        initFragmentAndDrawable()
        initViewPager()
    }

    private fun initTitleBar() {
        titleBarUIComponent.initTitle("我的卡包")
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary))
        titleBarUIComponent.initLeftBack { finish() }
    }

    private fun initViewPager() {
        acamViewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment {
                return mFragmentList[position]
            }

            override fun getCount(): Int {
                return mFragmentList.size
            }
        }

        acamTrackIndicatorView.setAdapter(object : IndicatorAdapter() {
            override fun getItemCount(): Int {
                return mFragmentList.size
            }

            override fun getView(position: Int, parent: ViewGroup): View {
                val tvTab: TextView = LayoutInflater.from(thisActivity()).inflate(R.layout.layout_loan_tab, parent, false) as TextView
                tvTab.text = mTitleList[position]
                return tvTab
            }

            override fun highLightIndicator(view: View) {
                val tvTab: TextView = view as TextView
                tvTab.isSelected = true
            }

            override fun resetIndicator(view: View) {
                val tvTab: TextView = view as TextView
                tvTab.isSelected = false
            }

            override fun getBottomTrackView(): View? {
                val view = View(thisActivity())
                view.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary))
                view.layoutParams = ViewGroup.LayoutParams(100.dp(), 2.dp())
                return view
            }
        }, acamViewPager)
    }

    private fun initFragmentAndDrawable() {
        val fragmentCredit = FragmentCredit()
        val fragmentSavings = FragmentDepositCard()
        mFragmentList.add(fragmentCredit)
        mFragmentList.add(fragmentSavings)
    }
}